/**
 * Created by guomin on 2017/5/4.
 */
var validator=require('validator');
var initPool=require('../common/initPool');
var checkParams=require('../common/checkParams');
var getData=require('../common/getData');
var jsonWrite=require('../conf/response');
var smsSend=require('../common/smsSend');

var resMsg=function(code,params){
    switch (code){
        case 0:return "success";break;
        case 101:return "参数不全";break;
        case 102:return "参数格式错误";break;
        case 103:return "图片验证码错误";break;
        case 104:return "手机号格式错误";break;
        case 105:return "参数为空";break;
        case 106:return "同一手机号一分钟内重复发送验证码";break;
        case 201:return "服务端异常";break;
        case 202:return "已加油该心愿";break;
        case 203:return "短信发送失败";break;
        case 205:return "心愿不存在";break;
        default:return "其它错误";break;
    }
};

module.exports={
    //心愿列表
    queryWishes:function(req,res){
        var date=new Date();
        var params=req.query;
        checkParams.isDefined(params,["pageNow","pageSize"]).then(function(){
            return checkParams.isParamsEmpty(params,["pageNow","pageSize"]);
        }).then(function(){
            return checkParams.isRightFormat(params,["pageNow","pageSize"]);
        }).then(function(){
            return initPool();
        }).then(function(connection){
            return getData.getWishes(connection,params);
        }).then(function(result){
            jsonWrite(res,{"status":0,"msg":resMsg(0),"result":result,"time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    },
    //分类列表
    queryClaimWishes:function(req,res){
    var date = new Date();
    var params = req.query;
    checkParams.isDefined(params,["pageNow","pageSize","status"]).then(function(){
      return checkParams.isParamsEmpty(params,["pageNow","pageSize","status"]);
    }).then(function(){
      return checkParams.isRightFormat(params,["pageNow","pageSize","status"]);
    }).then(function (){
      return initPool();
    }).then(function(connection){
      return getData.getClaimWishes(connection,params);
    }).then(function (result) {
      jsonWrite(res,{"status":0,"msg":resMsg(0),"result":result,"time":date.toLocaleString()})
    }).catch(function(err){
      if(err){
        var isInt=validator.isInt(err.toString());
        if(isInt){
          jsonWrite(res,{"status":err,"msg":resMsg(err),"time":date.toLocaleString()});
        }else{
          jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
          throw err;
        }
      }
    });
    },
    //心愿详情
    queryWishDetail:function(req,res){
        var date=new Date();
        var params=req.query;
        checkParams.isDefined(params,["wid","uid"]).then(function(){
            return checkParams.isParamsEmpty(params,["wid","uid"]);
        }).then(function(){
            return checkParams.isRightFormat(params,["wid","uid"]);
        }).then(function(){
            return initPool();
        }).then(function(connection){
            return getData.getWishDetail(connection,params);
        }).then(function(result){
            jsonWrite(res,{"status":0,"msg":resMsg(0),"result":result,"time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err,params),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    },
    //为TA加油
    likeWish:function(req,res){
        var date=new Date();
        var params=req.body;
        checkParams.isDefined(params,["code","wid"]).then(function(){
            return checkParams.isRightFormat(params,["wid"]);
        }).then(function(){
            return checkParams.isParamsEmpty(params,["wid","code"]);
        }).then(function(){
            return initPool();
        }).then(function(connection){
            return getData.likeWish(connection,params);
        }).then(function(){
            jsonWrite(res,{"status":0,"msg":"加油成功","time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    },
    //图片验证码验证
    checkCaptcha:function(req,res){
        var date=new Date();
        var params=req.query;
        checkParams.isDefined(params,["captcha"]).then(function(){
           return checkParams.isParamsEmpty(params,["captcha"]);
        }).then(function(){
            return checkParams.isRightCaptcha(req,params.captcha);
        }).then(function(){
            jsonWrite(res,{"status":0,"msg":"验证码正确","time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    },
    //短信验证码
    smsSend:function(req,res){
        var date=new Date();
        var params=req.body;
        console.log(params);
        checkParams.isDefined(params,["mobile","captcha"]).then(function(){
            return checkParams.isParamsEmpty(params,["mobile","captcha"]);
        }).then(function(){
            return checkParams.isRightCaptcha(req,params.captcha);
        }).then(function(){
            return checkParams.isMobile(params.mobile);
        }).then(function(){
            return smsSend.smsSend(req,params);
        }).then(function(body){
            if(body.status==0){
                jsonWrite(res,{"status":0,"msg":"SUCCESS","time":date.toLocaleString()});
            }else{
                jsonWrite(res,{"status":203,"msg":resMsg(203),"time":date.toLocaleString()});
            }
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });

    }
};